﻿using System;
using System.Collections.Generic;
using System.Linq;
using Data.Entity;
using Service;

namespace ECommerce.Controls
{
    public partial class ProductPictures : BaseUserControl
    {
		private IProductService _ProductService;
		private IPictureService _PictureService; 

        public Product TheProductDataSource
        {
			get
			{
				if(CommonHelper.QueryStringInt("productid") > 0)
				{
					if(_ProductService == null)
					{
						_ProductService = new ProductService();
					}

					Product theProduct = _ProductService.GetProduct(CommonHelper.QueryStringInt("productid"));

					return theProduct;
				}
				else
				{
					return null;
				}
			}
        }

		public int GetProductID()
		{
			return CommonHelper.QueryStringInt("productid");
		}

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Page_PreRender(object sender, EventArgs e)
        {
			if (!IsPostBack)
			{
				var pictures = GetProductPictures();

				RepeaterMorePictures.DataSource = pictures;
				RepeaterMorePictures.DataBind();

				// set the first picture
				if (pictures != null)
				{
					var theFirstPic = pictures.First();
				}
			}
        }

		private IEnumerable<Picture> GetProductPictures()
		{
			if (TheProductDataSource == null || TheProductDataSource.ProductID <= 0)
			{
				return null;
			}

			if (_PictureService == null)
			{
				_PictureService = new PictureService();
			}

			var pictures = _PictureService.GetPicturesByProduct(TheProductDataSource.ProductID);

			if (pictures != null)
			{
				return pictures.Take(6);
			}
			else
			{
				return null;
			}
		}
    }
}